﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Web.Framework;
using Web.CrmContacto.Code;
using CrmContacto.Contracts;
using Web.Framework.Translate;
using CrmContacto.WebApp.Code.InnerClasses;
using CrmContacto.Contracts.Contracts.Search;
using CrmContacto.Contracts.Contracts;
using CrmContacto.Contracts.Enums;
using Web.CrmContacto.Code.Security;
using Web.Framework.Helpers;
namespace CrmContacto.WebApp.App
{
    public partial class TipoMensajeBitacoraList : BasePage
    {
        private List<LinkButton> actionButtons = new List<LinkButton>();
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            LinkButton nuevaCategoriaButton = new LinkButton();
            nuevaCategoriaButton.ID = "BoCategoriasActionMenuNuevaCategoria";
            nuevaCategoriaButton.Text = LocalizationProvider.Instance["Bo.TipoMensajeBitacora.ActionMenu.NuevoTipo"];
            nuevaCategoriaButton.Click += new EventHandler(nuevTipoMensajeButton_Click);

            actionButtons.Add(nuevaCategoriaButton);
            base.LoadContextActions(actionButtons);

        }

        private void nuevTipoMensajeButton_Click(object sender, EventArgs e)
        {
            AgregarNuevoTipoMensaje();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                HasPermissionToSeeMe(CrmSecurity.BITACORA_MSGTYPE_MANAGE);
                this.lnkBuscar.Attributes.Add("aspnetid", this.lnkBuscar.ClientID);

                // HasPermissionToSeeMe(PlanProdSecurity.DESTINO_VER);
                if (!this.IsPostBack)
                {
                    LoadCbos();
                    Buscar();
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        private void LoadCbos()
        {
            UIHelper.LoadEstados<eSiNo>(this.cboSeUsaEnPDV, LocalizationProvider.Instance["DropDown.Todos"]);
        }

        protected override void ApplySecurity()
        {
            //this.btnAgregarNuevo.Visible = HasPermissionFor(PlanProdSecurity.DESTINO_CREAR);

            //if (this.GridCategorias.DataSource != null)
            //{
            //    this.GridCategorias.Columns[0].Visible = HasPermissionFor(PlanProdSecurity.DESTINO_EDITAR);
            //    this.GridCategorias.Columns[1].Visible = HasPermissionFor(PlanProdSecurity.DESTINO_ELIMINAR);
            //}
        }

        private void Buscar()
        {
            string nombreLike = this.txtNombre.Text;
            TipoMensajeBitacoraSearch tipoSearch = new TipoMensajeBitacoraSearch() { EmpresaId = BaseWebPage.CompanyId, Nombre = nombreLike };            
            int? siNo = UIHelper.GetIntFromInputCbo(this.cboSeUsaEnPDV);
            if (siNo.HasValue)
            {
                tipoSearch.EsPdv = (eSiNo)siNo == eSiNo.Si;
            }

            List<TipoMensajeBitacora> categoriaList = WebServiceHelper.GetTipoMensajeBitacora(tipoSearch);

            this.Grid.DataSource = categoriaList;
            this.Grid.DataBind();

            pnlResults.Visible = true;
        }

        protected void Grid_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    e.Row.Cells[0].Attributes.Add("title", "Editar");
                    e.Row.Cells[1].Attributes.Add("title", "Eliminar");
                    ImageButton btnEliminarCtrl = (ImageButton)e.Row.FindControl("btnEliminar");
                    SetJavascriptForDeleteRecord(btnEliminarCtrl, ((TipoMensajeBitacora)e.Row.DataItem).Nombre);                      
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected void Grid_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                if (e.CommandName == "editar")
                {
                    int tipoMensajeId = Convert.ToInt32(this.Grid.DataKeys[Convert.ToInt32(e.CommandArgument)].Value);
                    this.Response.Redirect(string.Format(Request.Url.Scheme + "://" + Request.Url.Authority + MiscClass.UrlTipoMensajeBitacoraEdit + "?Id={0}", tipoMensajeId), true);
                }
                else if (e.CommandName == "eliminar")
                {
                    int tipoMensajeId = Convert.ToInt32(this.Grid.DataKeys[Convert.ToInt32(e.CommandArgument)].Value);
                    WebServiceHelper.EliminarTipoMensajeBitacora(tipoMensajeId);
                    Buscar();
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected void Grid_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            try
            {
                this.Grid.PageIndex = e.NewPageIndex;

                Buscar();
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected void lnkBuscar_Click(object sender, EventArgs e)
        {
            try
            {
                Buscar();
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected void btnAgregarNuevo_Click(object sender, EventArgs e)
        {
            AgregarNuevoTipoMensaje();
        }

        private void AgregarNuevoTipoMensaje()
        {
            try
            {
                Response.Redirect(Request.Url.Scheme + "://" + Request.Url.Authority + MiscClass.UrlTipoMensajeBitacoraEdit, true);
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected void lnkExportarAExcel_Click(object sender, EventArgs e)
        {
            try
            {
                bool previousAllowPaging = this.Grid.AllowPaging;
                this.Grid.AllowPaging = false;

                Buscar();

                GridViewExportExcelUtil.Export("TipoMensajeBitacora.xls", this.Grid, 0, 1, 2);

                this.Grid.AllowPaging = previousAllowPaging;
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }
    }
}